﻿using System;
using System.Data;
using System.Xml;
using HPortal.Lib;
using System.Data.SqlClient;

namespace Web20.Components
{
    public partial class oMenu1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = new DataSet();
                ds.Tables.Add( DatabaseHelper.Select("SELECT [Name],[ChannelID],[ParentID] FROM [Channel] WHERE [Locked]=0 AND [ParentID] is not null ORDER BY [ParentID],[Priority];"));
                using (ds)
                {
                    using (DataTable dt = ds.Tables[0])
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            int parentID = (int)dr["ParentID"];
                            if (parentID == 1)
                                dr["ParentID"] = DBNull.Value;
                        }
                        dt.TableName = "Channel";
                        DataRelation relation = new DataRelation("ParentChild",
                            dt.Columns["ChannelID"],
                            dt.Columns["ParentID"],
                            true);
                        relation.Nested = true;
                        ds.Relations.Add(relation);
                        this.menu.DocumentContent = ds.GetXml();
                    }
                }
            }
        }
    }
}